<template>
  <!--
    style="padding: 10px"
    padding  内边距,上下左右各10px
  -->
  <div style="padding: 10px">
    <!-- 功能区   -->
    <!--
    style="margin: 10px 0"
    margin  外边距,上下10px左右0
    -->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
      <el-button type="primary">导出</el-button>
    </div>
    <!-- 搜索区 -->
    <div style="margin: 10px 0">
      <el-input placeholder="请输入关键字" v-model="search" clearable style="width: 20%"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>

    <!--
      <el-table :data="tableData" stripe style="width: 100%" border></el-table>
      stripe 表格条纹(斑马纹)
      border 实线
      <el-table-column prop="date" label="日期" width="180" sortable> </el-table-column>
      不加 width="180" 自适应
      sortable  排序
    -->
    <el-table :data="tableData" stripe style="width: 100%" border>
      <el-table-column prop="id" label="ID" sortable> </el-table-column>
      <el-table-column prop="username" label="姓名"> </el-table-column>
      <el-table-column prop="nickName" label="昵称"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="80">
        <template #default="scope">
          <el-tag  effect="dark" v-if="scope.row.sex==0">未知</el-tag>
          <el-tag  effect="dark" v-if="scope.row.sex==1">男</el-tag>
          <el-tag effect="dark" v-if="scope.row.sex==2">女</el-tag>

        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-switch v-model="scope.row.status" :active-value="1"
                     :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949"
          @change="updateStatus($event,scope.row.id)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="role" label="权限"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？"
                         size="medium"
                         @confirm="confirmEvent(scope.row.id)"
                         @cancel="cancelEvent">
            <template #reference>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--  分页  -->
    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>

    <!--  新增弹窗  -->

    <el-dialog title="新增用户" v-model="dialogFormVisible" width="30%">
      <el-form :model="form"
               label-width="100px"
               :rules="rules"
               ref="form"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" :label="0">男</el-radio>
          <el-radio v-model="form.sex" :label="1">女</el-radio>
          <el-radio v-model="form.sex" :label="2">未知</el-radio>
        </el-form-item>
        <el-form-item label="年龄" prop="age" :rules="[
      { required: true, message: '年龄不能为空'},
      { type: 'number', message: '年龄必须为数字值'}
    ]">
          <el-input
                  type="age"
                  v-model.number="form.age"
                  autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
<!--          <el-input v-model="form.status" autocomplete="off" style="width: 80%"></el-input>-->
          <el-radio v-model="form.status" :label="0">启用</el-radio>
          <el-radio v-model="form.status" :label="1">禁用</el-radio>
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 80%"
                     >
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="普通用户" value="普通用户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" v-model="form.address" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save('form')"
      >确 定</el-button
      >
    </span>
      </template>
    </el-dialog>
    <!--  修改弹窗  -->

    <el-dialog title="修改用户" v-model="dialogVisible" width="30%">
        <el-form :model="form"
                 :rules="rules"
                 ref="form"
        >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" :label="0">男</el-radio>
          <el-radio v-model="form.sex" :label="1">女</el-radio>
          <el-radio v-model="form.sex" :label="2">未知</el-radio>
        </el-form-item>
            <el-form-item label="年龄" prop="age" :rules="[
      { required: true, message: '年龄不能为空'},
      { type: 'number', message: '年龄必须为数字值'}
    ]">
                <el-input
                        type="age"
                        v-model.number="form.age"
                        autocomplete="off"
                ></el-input>
            </el-form-item>
        <el-form-item label="电话" >
          <el-input v-model="form.phone" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
            <el-form-item
                    prop="email"
                    label="邮箱"
                    :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
            >
                <el-input v-model="form.email"></el-input>
            </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 80%"
                     >
            <el-option label="管理员" value="管理员"></el-option>
            <el-option label="普通用户" value="普通用户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" v-model="form.address" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        </el-form>
      <template #footer>
    <span class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="savesave"
      >确 定</el-button
      >
    </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue' 例子,可删

import request from "../utils/request";

export default {
  name: 'Home',
  data() {
    return {
      search: '',
      dialogFormVisible: false,
      dialogVisible:false,
      formLabelWidth: '80px',
      form: {
        age:'',
        email:''

      },
      sex: 0,
      currentPage: 1,
      pageSize: 10,
      total: 10,
      tableData: null,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 8,
            message: '长度在 3 到 8 个字符',
            trigger: 'blur',
          }
        ],
        nickName:[
          { required: true, message: '请输入昵称', trigger: 'blur' },
          {
            min: 3,
            max: 8,
            message: '长度在 3 到 8 个字符',
            trigger: 'blur',
          }
        ],
        address:[
          { required: true, message: '请输入地址', trigger: 'blur' },
          {
            min: 3,
            max: 8,
            message: '长度在 3 到 8 个字符',
            trigger: 'blur',
          }
        ],
          phone:[
              {required: true,message:'请输入正确电话',trigger:'blur'},
              {

              }
          ]
      },
    }
  },
  methods: {
    /**
     * 数据加载
     */
    load() {
      request.get("/user/list",{
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    /**
     * 新增弹窗
     */
    add() {
      // 清空数据
      this.form = {};
      this.form.sex = 2;
      this.form.status = 0
      // 打开弹窗
      this.dialogFormVisible = true;

    },
    /**
     * 点击确认按钮 -> 新增/修改数据
     */
    save(formName) {

        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.form.id);
            // 新增操作
            request.post("/user/save",this.form).then(res => {
              console.log(res)      //输出控制台
              if (res.code === 20000) {
                this.$message({
                  type: "success",
                  message: "新增成功"
                })
              } else {
                this.$message({
                  type: "error",
                  message: res.msg
                })
              }

            })
            this.dialogFormVisible = false  // 关闭弹窗
          } else {
            console.log('error submit!!')
            return false
          }
        })
      {

      }
      this.load();
    },
    savesave(){
      if (this.form.id) { // 更新操作
        console.log("更新操作")
        request.post("/user/update",this.form).then(res => {
          console.log(res)
          if (res.code === 20000) {
            this.$message({
              type: "success",
              message: "修改成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.dialogVisible = false  // 关闭弹窗

        })
      }

    },
    /* 修改状态码  $event 触发事件,获取改变后的状态  num: id */
    updateStatus($event,num){
      // console.log($event)//0 禁用 1 启用 0 1
      // console.log(num)// 122642  122660
      let id = num;
      let status = $event;
      request.put("/user/updateStatus/"+id +"/" +status).then(res => {
        console.log(res)
      })
    },
    /**
     * 点击修改按钮 -> 数据回显/打开弹窗
     */
    handleEdit(row) {
      // JSON.parse()  是将字符串中的对象解析出来
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    /**
     * 气泡确认框 - 确认
     * 点击删除按钮 -> 确认 -> 通过id删除数据
     */
    confirmEvent(id) {
      console.log("确定删除")
      console.log(id)
      request.delete("/user/" + id).then(res => {
        console.log(res)
        if (res.code === 20000) {
          this.$message({
            type: "success",
            message: "删除数据成功"
          })
          this.load();
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    },
    /**
     * 气泡确认框 - 取消
     */
    cancelEvent() {
      this.$message({
        type: "error",
        message: "寻思好再删除,磨磨叽叽的"
      })
    },
    /**
     * 选择每页几条
     * @param val
     */
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    /**
     * 修改页码状态
     * @param val 第几页
     */
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    }
  },
  created(){
    this.load();
  },
  components: {
  }
}
</script>


